package com.gxuwz.medical.domain.chronicinfo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.chronicdis.*;
import com.gxuwz.medical.domain.person.Person;
import com.gxuwz.medical.tools.DateUtil;
/**
 * 慢病证信息管理模块类
 * @author 演示
 *
 */
public class ChronicInfo {
	/**
	 * 慢病档案编号=农合证号(14位)+建档时间（14位）
	 */
	private String id;
	/**
	 * 农合证号
	 */
    private Person person;
	/**
	 * 疾病编码
	 */
	private Chronicdis chronicdis;	
	/**
	 * 慢病起始时间
	 */
	private String starttime;
	/**
	 * 慢病治愈时间
	 */
	private String endtime;
	
	/**
	 * 状态:1表示正常、0表示注销
	 */
	private String status;
	/**
	 * 建档时间
	 */
	private java.util.Date creattime;
	/**
	 * 建档人
	 */
	private String creator;
	
	public ChronicInfo (){
		
	}
	/**
	 * 添加慢病证信息
	 * @throws Exception
	 */
	public void addChroInfo()throws Exception{
		Connection conn=null;
		try{
			
			Date creattime=new Date();
			String no=DateUtil.format(creattime, DateUtil.DFMT_YYYYMMDDHHMMSS);
			//慢病档案编号
			this.id=this.getPerson().getPerscode()+no;
			//1:获取连接实例
			conn=DbUtil.getConn();
		    //2:持久化数据
			saveToDB(conn);
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			//关闭连接
			DbUtil.close(conn);
		}
		
		
	}
	/**
	 * 持久化数据
	 * @param conn
	 * @throws SQLException
	 */
	private void saveToDB(Connection conn)throws SQLException{

		  PreparedStatement pstmt=null;
		  try{
			  StringBuffer sqlBuff=new StringBuffer("INSERT INTO t_chronicInfo (id,perscode, illcode, starttime, endtime,status,creattime,creator)");
			  			   sqlBuff.append("VALUES (?, ?, ?, ?,?,?,?,?)");
			  pstmt=conn.prepareStatement(sqlBuff.toString());
			  int index=1;
			  pstmt.setString(index++, this.id);
			  pstmt.setString(index++, this.getPerson().getPerscode());
			  pstmt.setString(index++, this.chronicdis.getIllcode());
			  pstmt.setString(index++, this.starttime);
			  pstmt.setString(index++, this.endtime);
			  pstmt.setString(index++, this.status);
			  pstmt.setDate(index++, new java.sql.Date(new Date().getTime()));
			  pstmt.setString(index++, this.creator);
			  int count =pstmt.executeUpdate(); 
		  }catch(SQLException e){
			  e.printStackTrace();
			  throw e;
		  }finally{
			  DbUtil.close(pstmt);
		  } 
}
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public Chronicdis getChronicdis() {
		return chronicdis;
	}
	public void setChronicdis(Chronicdis chronicdis) {
		this.chronicdis = chronicdis;
	}
	public String getStarttime() {
		return starttime;
	}
	public void setStarttime(String starttime) {
		this.starttime = starttime;
	}
	public String getEndtime() {
		return endtime;
	}
	public void setEndtime(String endtime) {
		this.endtime = endtime;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public java.util.Date getCreattime() {
		return creattime;
	}
	public void setCreattime(java.util.Date creattime) {
		this.creattime = creattime;
	}
	public String getCreator() {
		return creator;
	}
	public void setCreator(String creator) {
		this.creator = creator;
	}
    
	

}
